package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.pojo.CategorySecond;
import net.zjitc.service.CategorySecondService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.Impl.CategorySecondServiceImpl;
import net.zjitc.service.Impl.CategoryServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by dell on 2017/11/18.
 */
public class CategorySecondController {
    CategorySecondService service=new CategorySecondServiceImpl();
    CategoryService servicet=new CategoryServiceImpl();
    @URLMapping(url = "/findAllCategorySecond")
    public String findAllCategorySecond(HttpServletRequest request, HttpServletResponse response)
    {
        List<CategorySecond> list=service.findAllCategorySecond();
        request.setAttribute("list",list);
        return "/admin/categorysecond/list.jsp";
    }
    @URLMapping(url = "/CategorySecondAddUI")
    public String CategorySecondAddUI(HttpServletRequest request, HttpServletResponse response)
    {
        List<Category> list=servicet.findAllCategory();
        request.setAttribute("list",list);
        return "/admin/categorysecond/add.jsp";
    }
    @URLMapping(url = "/CategorySecondAdd")
    public String CategorySecondAdd(HttpServletRequest request, HttpServletResponse response)
    {
        CategorySecond categorySecond=new CategorySecond();
        Category category=new Category();
        try {
            BeanUtils.populate(category,request.getParameterMap());
            BeanUtils.populate(categorySecond,request.getParameterMap());
            categorySecond.setCategory(category);
            service.CategorySecondAdd(categorySecond);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/findAllCategorySecond";
    }
    @URLMapping(url = "/CategorySecondEditUI")
    public String CategorySecondEditUI(HttpServletRequest request, HttpServletResponse response)
    {
        String csid=request.getParameter("csid");
        CategorySecond categorySecond=service.findCategorySecondById(csid);
        request.setAttribute("cs",categorySecond);

        List<Category> list=servicet.findAllCategory();
        request.setAttribute("clist",list);

        return "/admin/categorysecond/edit.jsp";
    }
    @URLMapping(url = "/CategorySecondEdit")
    public String CategorySecondEdit(HttpServletRequest request, HttpServletResponse response)
    {
        CategorySecond categorySecond=new CategorySecond();
        Category category=new Category();
        try {
            BeanUtils.populate(category,request.getParameterMap());
            BeanUtils.populate(categorySecond,request.getParameterMap());
            categorySecond.setCategory(category);
            service.CategorySecondEdit(categorySecond);
        } catch (Exception e) {
            e.printStackTrace();
        }
         return "/findAllCategorySecond";
    }
    @URLMapping(url = "/CategorySecondDelete")
    public String CategorySecondDelete(HttpServletRequest request, HttpServletResponse response)
    {
        String csid=request.getParameter("csid");
        service.CategorySecondDelete(csid);
        return "/findAllCategorySecond";
    }
}
